package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.entity.Carousel;
import cn.stylefeng.guns.modular.business.mapper.CarouselMapper;
import cn.stylefeng.guns.modular.business.model.params.CarouselParam;
import cn.stylefeng.guns.modular.business.model.result.CarouselResult;
import  cn.stylefeng.guns.modular.business.service.CarouselService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 首页轮播图片管理 服务实现类
 * </p>
 *
 * @author pzp
 * @since 2019-09-10
 */
@Service
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    @Autowired
    private CarouselMapper carouselMapper;

    @Override
    public void add(CarouselParam param){
        Carousel entity = getEntity(param);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setDeleteFlag(RecoverConst.DEL_FLAG_FALSE);
        this.save(entity);
    }

    @Override
    public void delete(CarouselParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CarouselParam param){
        Carousel oldEntity = getOldEntity(param);
        Carousel newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CarouselResult findBySpec(CarouselParam param){
        return null;
    }

    @Override
    public List<CarouselResult> findListBySpec(CarouselParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CarouselParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CarouselParam param){
        return param.getCarouselId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Carousel getOldEntity(CarouselParam param) {
        return this.getById(getKey(param));
    }

    private Carousel getEntity(CarouselParam param) {
        Carousel entity = new Carousel();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 首页轮播图
     *
     * @return
     */
    @Override
    public List<CarouselResult> get() {
        return carouselMapper.get();
    }
}
